package com.umpay.config;

import com.umpay.exception.BusinessException;
import com.umpay.interceptor.LoggerInterceptor;
import com.umpay.interceptor.SignatureAuthInterceptor;
import com.umpay.interceptor.ValidatorInterceptor;
import com.umpay.util.SignatureAuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 注册拦截器
 *
 * @ClassName InterceptorConfiguration
 * @Description TODO
 * @author FENG JUN LIU
 * @Date 2018年1月9日
 * @version 1.0.0
 */
@Component
public class InterceptorConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private static ApplicationContextProvider applicationContextProvider;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 日志处理拦截器
        registry.addInterceptor(new LoggerInterceptor() {

            @Override
            protected String getSign(String data) {
                return SignatureAuth.getSignatureAuth().signMsg(data);
            }
        }).addPathPatterns("/**").excludePathPatterns("/**.html", "/**/file/upload/**");
        // 签名认证拦截器
        registry.addInterceptor(new SignatureAuthInterceptor() {

            @Override
            protected void signatureAuth(String sign, String data, String merId) throws BusinessException {
                SignatureAuth.getSignatureAuth().verifyMerSign(sign, data, merId);
            }

            @Override
            protected void signatureAuth(String sign, Map<String, String> data, String merId) throws BusinessException {
                SignatureAuth.getSignatureAuth().verifyMerSign(sign, data, merId);
            }

            @Override
            protected String getSign(String data) {
                return SignatureAuth.getSignatureAuth().signMsg(data);
            }

            @Override
            protected boolean isNotNeedSign(HttpServletRequest request) {

                return InterceptorConfiguration.this.isNotNeedSign(request);
            }

            @Override
            protected Map<String, String> signatureAuthAndRetMessage(String sign, String data, String merId)
                    throws BusinessException {
                return SignatureAuth.getSignatureAuth().verifyMerSignRetMessage(sign, data, merId);
            }

            @Override
            protected Map<String, String> signatureAuthAndRetMessage(String sign, Map<String, String> data, String merId)
                    throws BusinessException {
                return SignatureAuth.getSignatureAuth().verifyMerSignRetMessage(sign, data, merId);
            }
        }).addPathPatterns("/**").excludePathPatterns("/**.html", "/**/file/upload/**");
        // 参数校验拦截器
        registry.addInterceptor(new ValidatorInterceptor() {

            @Override
            protected void checkParam(Map<String, Object> map) throws BusinessException {
                // map类型校验规则，业务线自定义
            }

            @Override
            protected String getSign(String data) {

                return SignatureAuth.getSignatureAuth().signMsg(data);
            }
        }).addPathPatterns("/**").excludePathPatterns("/**.html", "/**/file/upload/**");
    }

    public static final class SignatureAuth {

        private static SignatureAuthUtil signatureAuthUtil;

        public static SignatureAuthUtil getSignatureAuth() {
            if (signatureAuthUtil == null) {
                signatureAuthUtil = (SignatureAuthUtil) applicationContextProvider.getApplicationContext()
                        .getBean("signatureAuthUtil");
            }
            return signatureAuthUtil;
        }

    }

    private boolean isNotNeedSign(HttpServletRequest request) {
        // http://localhost:8080/merAccess/recharge/shortcut

        String url = request.getRequestURI();
        if ("/merAccess/KeepAlive/".equals(url)) {
            return true;
        }
        if (url.contains("refund/refundcmd")) {
            return true;
        }
        if (url.contains("h5pay/backNotify")) {
            return true;
        }
        if (url.contains("h5pay/frontNotify")) {
            return true;
        }
        if (url.contains("register/mer/merDepInfReg")) {
            return true;
        }
        if (url.contains("/common/error")) {
            return true;
        }
        if (url.contains("/ebankpay/backNotify")) {
            return true;
        }
        if (url.contains("/ebankpay/frontNotify")) {
            return true;
        }
        if (url.contains("/payment/mposPay")) {
            return true;
        }
        if (url.contains("/transReport/analysisDetail")) {
            return true;
        }

        if (url.contains("/refund/refundOrderNotify")) {
            return true;
        }

        return false;

    }
}
